/**
 *
 */
package com.scs.application.core.utils;

import com.alibaba.fastjson.JSONArray;
import freemarker.template.*;
import lombok.extern.slf4j.Slf4j;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 * FreeMarkers工具类
 */
@Slf4j
public class FreeMarkers {

    private static final Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);

    static {
        configuration.setSharedVariable("vc", new ValidatorsCoverterMethod());
        configuration.setSharedVariable("uncamelCase", new UncamelCaseMethod());
        configuration.setSharedVariable("camelCase", new CamelCaseMethod());

    }


    /**
     * 模板名称字符串
     * @param templateString
     * @param model
     * @return
     */
    public static String renderString(String templateString, Map<String, ?> model) {
        try {
            StringWriter result = new StringWriter();
            Template t = new Template("name", new StringReader(templateString), configuration);
            t.process(model, result);

            return result.toString();
        } catch (Exception e) {
            throw Exceptions.unchecked(e);
        }
    }

    public static String renderTemplate(Template template, Object model) {
        try {
            StringWriter result = new StringWriter();
            template.process(model, result);
            return result.toString();
        } catch (Exception e) {
            throw Exceptions.unchecked(e);
        }
    }

    private static final class ValidatorsCoverterMethod implements TemplateMethodModelEx {

        @Override
        public Object exec(List arguments) throws TemplateModelException {

            if (arguments.size() != 2) {
                return "";
            }
            Object o1 = arguments.get(0);
            if (!(o1 instanceof SimpleNumber)) {
                return "";
            }

            Object o2 = ((DefaultListAdapter) arguments.get(1)).getWrappedObject();
            if (!(o2 instanceof JSONArray)) {
                return "";
            }


            JSONArray array = (JSONArray) o2;

            if (((SimpleNumber) o1).getAsNumber().intValue() != 1) {
                array.add("required");
            }

            return StringUtils.join(array.toArray(), "|");
        }
    }

    private static final class UncamelCaseMethod implements TemplateMethodModelEx {
        @Override
        public Object exec(List arguments) throws TemplateModelException {

            if (arguments.size() != 1) {
                return "";
            }
            String o1 = ((SimpleScalar) arguments.get(0)).getAsString();
            return StringUtils.uncamelCase(o1 == null ? "" : o1);
        }
    }
    private static final class CamelCaseMethod implements TemplateMethodModelEx {
        @Override
        public Object exec(List arguments) throws TemplateModelException {

            if (arguments.size() != 1) {
                return "";
            }
            Object o1 = ((SimpleScalar) arguments.get(0)).getAsString();
            return StringUtils.camelCase(o1 == null ? "" : o1.toString());
        }
    }

}
